package traveldream.ejb.entities;

import travelDream.ejb.DTOs.UserDTO;
import traveldream.ejb.entities.Group;

import java.io.Serializable;
import java.lang.String;
import java.util.List;

import javax.persistence.*;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * Entity implementation class for Entity: User
 *
 */
@Entity
@Table(name="USERS")
@NamedQuery(name= User.FIND_BY_EMAIL, query="SELECT u FROM User u WHERE u.email = :email")
public class User implements Serializable {

	//Utilizzabile dagli EJB per creare le queries
	public static final String FIND_BY_EMAIL = "User.findByEmail";  
	
	@Id
	private String email;
	@NotEmpty
	private String password;
	@NotEmpty
	private String nome;
	@NotEmpty
	private String cognome;
	private String indirizzo;
	
	private static final long serialVersionUID = 1L;
	
	//relazione many-to-many con Group
	@ManyToMany
	@JoinTable(name="USER_GROUP", 		
		joinColumns={@JoinColumn(name="EMAIL", referencedColumnName="EMAIL")},
		inverseJoinColumns={@JoinColumn(name="GROUPID", referencedColumnName="GROUPID")}
		)
	private List<Group> groups;
	
	@ManyToMany(mappedBy="partecipanti")	//relazione (inversa) many-to-many con Prenotazione
	private List<Prenotazione> prenotazioni;
	
	//costruttore default
	public User() {
		super();
	}
	
	//crea User da UserDTO
	public User(UserDTO user) {
		this.setEmail(user.getEmail());
    	this.setPassword(user.getPassword());
    	this.setNome(user.getNome());
    	this.setCognome(user.getCognome());
    	this.setIndirizzo(user.getIndirizzo());
	}

	//getters e setters
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}  
	
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}  
	
	public String getNome() {
		return this.nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}   
	
	public String getCognome() {
		return this.cognome;
	}

	public void setCognome(String cognome) {
		this.cognome = cognome;
	}   
	
	public String getIndirizzo() {
		return this.indirizzo;
	} 

	public void setIndirizzo(String indirizzo) {
		this.indirizzo = indirizzo;
	}
	
	public List<Group> getGroups() {
		return this.groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}
	
	//converte in UserDTO, utilizzabile da EJB per mandare dati al Web
	public UserDTO entityToDTO(){
		UserDTO tmp = new UserDTO();
		tmp.setEmail(email);
		//scelgo di non copiare la password in UserDTO
		tmp.setNome(nome);
		tmp.setCognome(cognome);
		tmp.setIndirizzo(indirizzo);
		return tmp;
	}


}
